<?php

require_once 'Diff/simplediff.php';
require_once './application/models/Objets/Composant.php';
require_once './application/entity/config/dataconfdiff.php';

class diffCore extends CoreBase
{

    private $_obj;
    private $_confId;
    private $_confId2;
    private $_conf;
    private $_html;

    public function getDiffConf()
    {
        $this->_confId = $this->_controller->getRequest()->getParam('idconf');
        $this->_conf = $this->getConfBase($this->_confId);
        if (isset($this->_conf[1]))
            $this->_html = $this->htmlDiff($this->_conf[1]['Configuration'], $this->_conf[0][0]['Configuration']);
        else
            return;
        $this->_obj = new confdiffData();
        $this->_obj->result = $this->_html;
        echo Zend_Json_Encoder::encode($this->_obj);
    }

    public function diffConf()
    {
        $this->_confId = $this->_controller->getRequest()->getParam('idconf');
        $this->_confId2 = $this->_controller->getRequest()->getParam('idconf2');
        $this->_obj = new ObjectComposant();
        $this->_conf[0] = $this->_obj->rechercheConfigurationId($this->_confId);
        $this->_conf[1] = $this->_obj->rechercheConfigurationid($this->_confId2);
        $this->_html = $this->htmlDiff($this->_conf[1][0]['Configuration'], $this->_conf[0][0]['Configuration'], true);
        $this->_obj = new confdiffData();
        $this->_obj->result = $this->_html;
        echo Zend_Json_Encoder::encode($this->_obj);
    }

    private function getConfBase()
    {
        $this->_obj = new ObjectComposant();
        $this->_conf[0] = $this->_obj->rechercheConfigurationId($this->_confId);
        $this->_conf[1] = $this->_obj->rechercheConfigurationDernierId($this->_confId);
        return $this->_conf;
    }

    private function htmlDiff($old, $new, $date = false)
    {
        $diff = simpleDiff::diff_to_array(false, $old, $new, true);

        $this->_html = '<table class="diff">';
        $this->_html .= '<tr><th/><th/>';
        if ($date == false)
            $this->_html .= '<th>' . $this->_conf[1]['Date'] . '</th><th/>';
        else
            $this->_html .= '<th>' . $this->_conf[1][0]['Date'] . '</th><th/>';
        $this->_html .= '<th>' . $this->_conf[0][0]['Date'] . '</th></tr>';

        $prev = key($diff);

        foreach ($diff as $i => $line)
        {
            if ($i > $prev + 1)
            {
                $this->_html .= '<tr><td colspan="5" class="separator"><hr /></td></tr>';
            }

            list( $type, $old, $new ) = $line;

            $class1 = $class2 = '';
            $t1 = $t2 = '';

            if ($type == simpleDiff::INS)
            {
                $class2 = 'ins';
                $t2 = '+';
            } elseif ($type == simpleDiff::DEL)
            {
                $class1 = 'del';
                $t1 = '-';
            } elseif ($type == simpleDiff::CHANGED)
            {
                $class1 = 'del';
                $class2 = 'ins';
                $t1 = '-';
                $t2 = '+';
                $this->parseDiff($old, $new);
            }

            $this->_html .= '<tr>';
            $this->_html .= '<td class="line">' . ($i + 1) . '</td>';
            $this->_html .= '<td class="leftChange">' . $t1 . '</td>';
            $this->_html .= '<td class="leftText ' . $class1 . '">' . $old . '</td>';
            $this->_html .= '<td class="rightChange">' . $t2 . '</td>';
            $this->_html .= '<td class="rightText ' . $class2 . '">' . $new . '</td>';
            $this->_html .= '</tr>';

            $prev = $i;
        }

        $this->_html .= '</table>';
        return $this->_html;
    }

    private function parseDiff($old, $new)
    {
        $lineDiff = simpleDiff::wdiff($old, $new);

        // Don't show new things in deleted line
        $old = preg_replace('!\{\+(?:.*)\+\}!U', '', $lineDiff);
        $old = str_replace('  ', ' ', $old);
        $old = str_replace('-] [-', ' ', $old);
        $old = preg_replace('!\[-(.*)-\]!U', '<del>\\1</del>', $old);

        // Don't show old things in added line
        $new = preg_replace('!\[-(?:.*)-\]!U', '', $lineDiff);
        $new = str_replace('  ', ' ', $new);
        $new = str_replace('+} {+', ' ', $new);
        $new = preg_replace('!\{\+(.*)\+\}!U', '<ins>\\1</ins>', $new);
    }

    public function deleteconf()
    {
        $this->_confId = $this->_controller->getRequest()->getParam('idconf');
        $this->_obj = new ObjectComposant();
        $this->_obj->supprimeConfiguration($this->_confId);
    }

}